package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;


public class Intonaco extends JPanel {

	private String[] elIntonaco = { "", "calce", "cementizio", "base calce", "base cementizio", "macro poroso", "micro poroso", "non rilevabile",
			"non presente", "tradizionale", "idrorepellente", "altro" };
	private String[] elPittura = { "", "silicati", "silossanica", "a calce", "elastomerica", "graffiato", "acrilica", "base quarzo", "non presente ",
			"non rilevabile", "cappotto termico", "idropittura", "altro" };

	private JComboBox intonacoEsterno = new JComboBox(elIntonaco);
	private boolean modifyIntonEst = false;
	private JTextField otherIntonEst = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private JComboBox pitturaEsterno = new JComboBox(elPittura);
	private boolean modifyPittEst = false;
	private JTextField otherPittEst = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private JTextPane note = new JTextPane() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField annoLEst = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private StartingWindow st;

	private LatoIntonacoPanel latoIntonaco;

	public Intonaco(StartingWindow st) {
		super();
		this.st = st;
		latoIntonaco = new LatoIntonacoPanel(st);
		setOpaque(false);
		setLayout(new MigLayout());
		RoundedPanel lato = new RoundedPanel("Lato");
		setCell(lato);
		RoundedPanel latoEsterno = new RoundedPanel("Esterno");
		setCell(latoEsterno);
		RoundedPanel latoInterno = new RoundedPanel("Interno fuori terra");
		setCell(latoInterno);
		RoundedPanel latoInterrato = new RoundedPanel("Interno interrato");
		setCell(latoInterrato);
		RoundedPanel noteLabel = new RoundedPanel("Note");
		setCell(noteLabel);
		RoundedPanel elIntonaco = new RoundedPanel("Tipologia intonaco");
		setCell(elIntonaco);
		RoundedPanel tipoPitture = new RoundedPanel("Tipologia pitture");
		setCell(tipoPitture);
		RoundedPanel anno = new RoundedPanel("Anno");
		setCell(anno);
		anno.setPreferredSize(new Dimension(120, 45));
		RoundedPanel piano = new RoundedPanel("Piano");
		setCell(piano);
		piano.setPreferredSize(new Dimension(100, 45));

		setCell();
		altroActionListener();
		addMouse();

		add(lato, "cell 0 0 , gapleft 10");
		add(elIntonaco, "cell 1 0");
		add(tipoPitture, "cell 2 0");
		add(anno, "cell 3 0");
		add(piano, "cell 4 0");
		add(latoEsterno, "cell 0 1, gapleft 10");
		add(intonacoEsterno, "cell 1 1");
		add(pitturaEsterno, "cell 2 1");
		add(annoLEst, "cell 3 1");
		add(latoIntonaco, "cell 0 4 5 1 , wrap");
		add(noteLabel);
		add(note, "span");

	}

	public void addMouse() {
		MouseAdapter mlA1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(otherIntonEst);
					intonacoEsterno.setSelectedIndex(0);
					add(intonacoEsterno, "cell 1 1");
					modifyIntonEst = false;
					paintAll(getGraphics());
					// st.refresh();
				}
			}
		};
		otherIntonEst.addMouseListener(mlA1);

		MouseAdapter mlA2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(otherPittEst);
					pitturaEsterno.setSelectedIndex(0);
					add(pitturaEsterno, "cell 2 1");
					modifyPittEst = false;

					paintAll(getGraphics());
				}
			}
		};
		otherPittEst.addMouseListener(mlA2);

	}

	public void setCell() {
		annoLEst.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		note.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		otherIntonEst.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		otherPittEst.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

		intonacoEsterno.setFont(new Font("Arial", Font.PLAIN, 16));
		pitturaEsterno.setFont(new Font("Arial", Font.PLAIN, 16));
		annoLEst.setFont(new Font("Arial", Font.PLAIN, 16));
		note.setFont(new Font("Arial", Font.PLAIN, 16));
		otherIntonEst.setFont(new Font("Arial", Font.PLAIN, 16));
		otherPittEst.setFont(new Font("Arial", Font.PLAIN, 16));

		intonacoEsterno.setPreferredSize(new Dimension(210, 45));
		pitturaEsterno.setPreferredSize(new Dimension(210, 45));
		annoLEst.setPreferredSize(new Dimension(120, 45));
		note.setPreferredSize(new Dimension(553, 45));
		otherIntonEst.setPreferredSize(new Dimension(210, 45));
		otherPittEst.setPreferredSize(new Dimension(210, 45));

		annoLEst.setOpaque(false);
		note.setOpaque(false);
		otherIntonEst.setOpaque(false);
		otherPittEst.setOpaque(false);

	}

	public void setCell(JComponent c) {
		// c.setBorder(new OvalBorder());
		c.setFont(new Font("Arial", Font.PLAIN, 16));
		c.setPreferredSize(new Dimension(210, 45));
		c.setOpaque(false);
		// c.setBackground(new Color(238, 131, 107));
	}

	public void altroActionListener() {
		intonacoEsterno.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((String) intonacoEsterno.getSelectedItem()).equalsIgnoreCase("altro")) {
					remove(intonacoEsterno);
					add(otherIntonEst, "cell 1 1");
					modifyIntonEst = true;
					st.refresh();
				}
			}
		});

		pitturaEsterno.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((String) pitturaEsterno.getSelectedItem()).equalsIgnoreCase("altro")) {
					remove(pitturaEsterno);
					add(otherPittEst, "cell 2 1");
					modifyPittEst = true;
					st.refresh();
				}
			}
		});

	}

	public String print() {
		String print = "\n#";
		if (modifyIntonEst)
			print = print + otherIntonEst.getText() + "\n#";
		else
			print = print + intonacoEsterno.getSelectedIndex() + "\n#";

		if (modifyPittEst)
			print = print + otherPittEst.getText() + "\n#";
		else
			print = print + pitturaEsterno.getSelectedIndex() + "\n#";
		print = print + annoLEst.getText();

		print = print + latoIntonaco.print();

		print = print + "\n#" + note.getText();
		return print;
	}

	public void setLatoIntonaco(String data) {
		latoIntonaco.setElement(data);
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		c.nextLine();
		String tmp = c.nextLine();
		try {
			intonacoEsterno.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			otherIntonEst.setText(tmp);
			remove(intonacoEsterno);
			add(otherIntonEst, "cell 1 1");
			modifyIntonEst = true;
		}
		tmp = c.nextLine();
		try {
			pitturaEsterno.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			otherPittEst.setText(tmp);
			remove(pitturaEsterno);
			add(otherPittEst, "cell 2 1");
			modifyPittEst = true;
		}

		try {
			annoLEst.setText(c.nextLine());
		} catch (Exception e) {
			annoLEst.setText("");
		}
	}

	public void setNote(String data) {
		note.setText(data);
	}

}
